package com.xiaobai.redisContact.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.RedissonRedLock;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequiredArgsConstructor
@RequestMapping("phb")
@Tag(name = "排行榜")
public class PhbController {

    private final RedisTemplate<String, String> redisTemplate;
    private final String PHB = "phb";

    @PostMapping("/login")
    @Operation(description = "登录")
    public String login(String user) {
        redisTemplate.opsForZSet().add(PHB, user, 0);
        return "登录成功";
    }

    @GetMapping
    @Operation(description = "获取数据")
    public Object getList() {
        Set<ZSetOperations.TypedTuple<String>> typedTuples = redisTemplate.opsForZSet().reverseRangeWithScores(PHB, 0, -1);
        Set<String> strings = redisTemplate.opsForZSet().rangeByScore(PHB, 0, -1);
        System.out.println(strings);
        return typedTuples;
    }

    @PostMapping("add")
    @Operation(description = "加分")
    public Object add(String user, Integer fen) {
        return redisTemplate.opsForZSet().incrementScore(PHB, user, fen);
    }



}
